//
//  TYZPageScrollMenuView.m
//  AVfoundationDemo
//
//  Created by TangBin on 2019/8/14.
//  Copyright © 2019 TangBin. All rights reserved.
//

#import "TYZPageScrollMenuView.h"
#import "TYZPageConfigration.h" // 配置
#import "TYZPageScrollView.h"

#define kTYZPageScrollMenuViewConverMarginX (5)

#define kTYZPageScrollMenuViewConverMarginW (10)

@interface TYZPageScrollMenuView ()

@property (nonatomic, strong) UIView *lineView; ///< line指示器

@property (nonatomic, strong) UIView *converView; ///< 蒙层

@property (nonatomic, strong) TYZPageScrollView *scrollView;

@property (nonatomic, strong) UIView *bottomLine; ///< 底部线条

@property (nonatomic, strong) TYZPageConfigration *configration; ///< 配置信息

@property (nonatomic, weak) id<TYZPageScrollMenuViewDelegate> delegate; ///< 代理

@property (nonatomic, assign) NSInteger lastIndex; ///< 上次的index

@property (nonatomic, assign) NSInteger currentIndex; ///< 当前的index

@property (nonatomic, strong) NSMutableArray<UIButton *> *itemsArray; ///< item数组

@property (nonatomic, strong) NSMutableArray<UILabel *> *badgesArray; ///< 未读消息的bagde

@property (nonatomic, strong) NSMutableArray *itemsWidthArray; ///< item的宽度
@end

@implementation TYZPageScrollMenuView

#pragma mark - private methods
- (void)setupSubViews
{
    self.backgroundColor = self.configration.scrollViewBackgroundColor;
    [self setupItems];
    [self setupOtherViews];
}

- (void)setupItems
{
    if (self.configration.buttonArray.count > 0 && self.titles.count == self.configration.buttonArray.count)
    {
        [self.configration.buttonArray enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self setupButton:obj title:self.titles[idx] index:idx];
        }];
    }
    else
    {
        [self.titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [self setupButton:itemBtn title:obj index:idx];
        }];
    }
}

- (void)setupOtherViews
{
    // showAddButton 如果这个为YES 表示有一个按钮，scrollView的宽度，要加强这个addbutton的宽度
    self.scrollView.frame = CGRectMake(0.0, 0.0, self.configration.showAddButton?self.width-self.height:self.width, self.height);
    [self addSubview:self.scrollView];
    
    if (self.configration.showAddButton)
    {
        self.addButton.frame = CGRectMake(self.width - self.height, 0, self.height, self.height);
        [self addSubview:self.addButton];
    }
    
    // item
    __block CGFloat itemX = 0;
    __block CGFloat itemY = 0;
    __block CGFloat itemW = 0;
    __block CGFloat itemH = self.height - self.configration.lineHeight;
    
    // 设置按钮的位置(最原始的位置 从左边到右边)
    [self.itemsArray enumerateObjectsUsingBlock:^(UIButton * _Nonnull btn, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 0)
        {
            itemX += self.configration.itemLeftAndRightMargin;
        }
        else
        {
            itemX += self.configration.itemMargin + [self.itemsWidthArray[idx-1] floatValue];
        }
        btn.frame = CGRectMake(itemX, itemY, [self.itemsWidthArray[idx] floatValue], itemH);
//        debugLogFrame(btn.frame);
//        btn.backgroundColor = [UIColor magentaColor];
        if (self.configration.isBadge)
        {
            // badge
            UILabel *badgeLabel = self.badgesArray[idx];
            badgeLabel.centerX = btn.right;
            badgeLabel.centerY = btn.top + 10;
        }
    }];
    
    CGFloat scrollSizeWidth = self.configration.itemLeftAndRightMargin + CGRectGetMaxX([self.itemsArray.lastObject frame]);
//    debugLog(@"maxX=%f; minX=%f", CGRectGetMaxX([self.itemsArray.lastObject frame]), CGRectGetMinX([self.itemsArray.lastObject frame]));
    if (scrollSizeWidth < self.scrollView.width)
    {// 没有超出宽度
        itemX = 0;
        itemY = 0;
        itemW = 0;
        CGFloat allItemWidth = 0; // 所有item的宽度
        for (NSNumber *width in self.itemsWidthArray)
        {
            allItemWidth += [width floatValue];
        }
        
        // 其实得到的是左边和右边的space (总的宽度-所有按钮的宽度和-所有item的space)*0.5
        CGFloat left = (self.scrollView.width - allItemWidth - self.configration.itemMargin * (self.itemsWidthArray.count-1)) * 0.5;
        
        // 居中且有剩余间距
        if (self.configration.aligmentModeCenter && left >= 0)
        {
            [self.itemsArray enumerateObjectsUsingBlock:^(UIButton * _Nonnull btn, NSUInteger idx, BOOL * _Nonnull stop) {
                if (idx == 0)
                {
                    itemX += left;
                }
                else
                {
                    itemX += self.configration.itemMargin + [self.itemsWidthArray[idx-1] floatValue];
                }
                btn.frame = CGRectMake(itemX, itemY, [self.itemsWidthArray[idx] floatValue], itemH);
            }];
            self.scrollView.contentSize = CGSizeMake(left + CGRectGetMaxX([self.itemsArray.lastObject frame]), self.scrollView.height);
        }
        else
        {// 不能滚动则平分
            if (!self.configration.scrollMenu)
            {
                [self.itemsArray enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger idx, BOOL * _Nonnull stop) {
                    itemW = self.scrollView.width / self.itemsArray.count;
                    itemX = itemW *idx;
                    button.frame = CGRectMake(itemX, itemY, itemW, itemH);
                }];
                
                self.scrollView.contentSize = CGSizeMake(CGRectGetMaxX([[self.itemsArray lastObject] frame]), self.scrollView.height);
                
            }
            else
            {
                self.scrollView.contentSize = CGSizeMake(scrollSizeWidth, self.scrollView.height);
            }
        }
    }
    else
    {
        self.scrollView.contentSize = CGSizeMake(scrollSizeWidth, self.scrollView.height);
    }
    
    
    CGFloat lineX = self.itemsArray.firstObject.frame.origin.x;
    CGFloat lineY = self.scrollView.height - self.configration.lineHeight;
    CGFloat lineW = self.itemsArray.firstObject.width;
    CGFloat lineH = self.configration.lineHeight;
    
    // 处理line宽度等于字体宽度
    if (!self.configration.scrollMenu && !self.configration.aligmentModeCenter && self.configration.lineWidthEqualFontWidth)
    {
        lineX = self.itemsArray.firstObject.frame.origin.x + (self.itemsArray.firstObject.width - [self.itemsWidthArray.firstObject floatValue]) * 0.5;
        lineW = [self.itemsWidthArray.firstObject floatValue];
    }
    
    // conver
    if (self.configration.showConver)
    {
        self.converView.frame = CGRectMake(lineX - kTYZPageScrollMenuViewConverMarginX, (self.scrollView.height-self.configration.converHeight-self.configration.lineHeight)*0.5, lineW+kTYZPageScrollMenuViewConverMarginW, self.configration.converHeight);
        [self.scrollView insertSubview:self.converView atIndex:0];
    }
    
    // bottomLine
    if (self.configration.showBottomLine)
    {
        self.bottomLine = [[UIView alloc] init];
        self.bottomLine.backgroundColor = self.configration.bottomLineBgColor;
        self.bottomLine.frame = CGRectMake(self.configration.bottomLineLeftAndRightMargin, self.height - self.configration.bottomLineHeight, self.scrollView.width - 2 * self.configration.bottomLineLeftAndRightMargin, self.configration.bottomLineHeight);
        self.bottomLine.layer.cornerRadius = self.configration.bottomLineCorner;
        [self insertSubview:self.bottomLine atIndex:0];
    }
    
    
    // scrollLine
    if (self.configration.showScrollLine)
    {
        self.lineView.frame = CGRectMake(lineX - self.configration.lineLeftAndRightAddWidth + self.configration.lineLeftAndRightMargin, lineY - self.configration.lineBottomMargin, lineW + self.configration.lineLeftAndRightAddWidth * 2 - 2 * self.configration.lineLeftAndRightMargin, lineH);
        self.lineView.layer.cornerRadius = self.configration.lineCorner;
        [self.scrollView addSubview:self.lineView];
    }
    
    if (self.configration.itemMaxScale > 1)
    {
        ((UIButton *)self.itemsArray[self.currentIndex]).transform = CGAffineTransformMakeScale(self.configration.itemMaxScale, self.configration.itemMaxScale);
    }
    [self setDefaultTheme];
    [self selectedItemIndex:self.currentIndex animated:NO];
}

- (void)setupButton:(UIButton *)itemButton title:(NSString *)title index:(NSInteger)index
{
    itemButton.titleLabel.font = self.configration.selectedItemFont;
    [itemButton setTitleColor:self.configration.normalItemColor forState:UIControlStateNormal];
    [itemButton setTitle:title forState:UIControlStateNormal];
    itemButton.tag = index;
    [itemButton addTarget:self action:@selector(itemButtonOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [itemButton sizeToFit];
    [self.itemsWidthArray addObject:@(itemButton.width)];
    [self.itemsArray addObject:itemButton];
    [self.scrollView addSubview:itemButton];
    
    // 是否显示标记
    if (self.configration.isBadge)
    {
        CGRect frame = CGRectMake(0, 0, 26, 16);
        UILabel *badgeLabel = [UILabel createWithLabel:self.scrollView frame:frame font:FONTSIZE(10) propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor whiteColor];
            label.textAlignment = NSTextAlignmentCenter;
            label.backgroundColor = [UIColor colorWithHexString:@"#FF3333"];
            label.text = @"0";
            label.hidden = YES;
            label.layer.borderColor = [UIColor whiteColor].CGColor;
            label.layer.borderWidth = 1;
        }];
        badgeLabel.layer.cornerRadius = frame.size.height * 0.5;
        badgeLabel.layer.masksToBounds = YES;
        [self.badgesArray addObject:badgeLabel];
    }
}


- (void)setDefaultTheme
{
    UIButton *currentButton = self.itemsArray[self.currentIndex];
    /// 缩放
    if (self.configration.itemMaxScale > 1)
    {
        currentButton.transform = CGAffineTransformMakeScale(self.configration.itemMaxScale, self.configration.itemMaxScale);
    }
    /// 颜色
    currentButton.selected = YES;
    [currentButton setTitleColor:self.configration.selectedItemColor forState:UIControlStateNormal];
    currentButton.titleLabel.font = self.configration.selectedItemFont;
    /// 线条
    if (self.configration.showScrollLine)
    {
        if (self.configration.lineWidth != 0)
        {
            self.lineView.left = currentButton.left + (currentButton.width - self.configration.lineWidth) * 0.5;
            self.lineView.width = self.configration.lineWidth;
        }
        else
        {
            self.lineView.left = currentButton.left - self.configration.lineLeftAndRightAddWidth + self.configration.lineLeftAndRightMargin;
            self.lineView.width = currentButton.width + self.configration.lineLeftAndRightAddWidth *2 - self.configration.lineLeftAndRightMargin * 2;
        }
        
        /// 处理Line宽度等于字体宽度
        if (!self.configration.scrollMenu &&
            !self.configration.aligmentModeCenter &&
            self.configration.lineWidthEqualFontWidth)
        {
            if (self.configration.lineWidth != 0)
            {
                self.lineView.left = currentButton.left + (currentButton.width - self.configration.lineWidth) * 0.5;
                self.lineView.width = self.configration.lineWidth;
            }
            else
            {
                self.lineView.left = currentButton.left + ([currentButton width]  - ([self.itemsWidthArray[currentButton.tag] floatValue])) / 2 - self.configration.lineLeftAndRightAddWidth - self.configration.lineLeftAndRightAddWidth;
                self.lineView.width = [self.itemsWidthArray[currentButton.tag] floatValue] + self.configration.lineLeftAndRightAddWidth *2;
            }
        }
    }
    /// 遮盖
    if (self.configration.showConver)
    {
        self.converView.left = currentButton.left - kTYZPageScrollMenuViewConverMarginX;
        self.converView.width = currentButton.width +kTYZPageScrollMenuViewConverMarginW;
        /// 处理conver宽度等于字体宽度
        if (!self.configration.scrollMenu &&
            !self.configration.aligmentModeCenter &&
            self.configration.lineWidthEqualFontWidth)
        {
            self.converView.left = currentButton.left + ([currentButton width]  - ([self.itemsWidthArray[currentButton.tag] floatValue])) / 2 - kTYZPageScrollMenuViewConverMarginX;
            self.converView.width = [self.itemsWidthArray[currentButton.tag] floatValue] + kTYZPageScrollMenuViewConverMarginW;
        }
    }
    self.lastIndex = self.currentIndex;
}

/**
 点击item事件

 @param sender sender description
 */
- (void)itemButtonOnClick:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    self.currentIndex = btn.tag;
    [self adjustItemWithAnimated:YES];
    if (self.delegate && [self.delegate respondsToSelector:@selector(pageScrollMenuViewItemOnClick:index:)])
    {
        [self.delegate pageScrollMenuViewItemOnClick:btn index:self.lastIndex];
    }
}

- (void)addButtonAction:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(pageScrollMenuViewAddButtonAction:)])
    {
        [self.delegate pageScrollMenuViewAddButtonAction:sender];
    }
}

#pragma mark - setter getter 懒加载
- (UIView *)lineView
{
    if (!_lineView)
    {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = self.configration.lineColor;
    }
    return _lineView;
}

- (UIView *)converView
{
    if (!_converView)
    {
        _converView = [[UIView alloc] init];
        _converView.layer.backgroundColor = self.configration.converColor.CGColor;
        _converView.layer.cornerRadius = self.configration.coverCornerRadius;
        _converView.layer.masksToBounds = YES;
        _converView.userInteractionEnabled = NO;
    }
    return _converView;
}

- (TYZPageScrollView *)scrollView
{
    if (!_scrollView)
    {
        _scrollView = [[TYZPageScrollView alloc] init];
        _scrollView.pagingEnabled = NO; // 是否使用分页机制，默认值为NO。当设置为YES时，会按照scrollView的宽度对内容视图进行分页。
        _scrollView.bounces = self.configration.bounces; // 弹簧效果
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.scrollEnabled = self.configration.scrollMenu;
    }
    return _scrollView;
}

- (UIButton *)addButton
{
    if (!_addButton)
    {
        _addButton = [[UIButton alloc] init];
        [_addButton setBackgroundImage:[UIImage imageNamed:self.configration.addButtonNormalImageName] forState:UIControlStateNormal];
        [_addButton setBackgroundImage:[UIImage imageNamed:self.configration.addButtonHightImageName] forState:UIControlStateHighlighted];
        _addButton.layer.shadowColor = [UIColor grayColor].CGColor;
        _addButton.layer.shadowOffset = CGSizeMake(-1, 0);
        _addButton.layer.shadowOpacity = 0.5;
        _addButton.backgroundColor = self.configration.addButtonBackgroundColor;
        [_addButton addTarget:self action:@selector(addButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

#pragma mark - public methods
/**
 初始化
 
 @param frame 大小
 @param titles 标题数组
 @param config 配置信息
 @param delegate 代理
 @param currentIndex 当前选中下标
 @return return value description
 */
+ (instancetype)pageScrollMenuViewWithFrame:(CGRect)frame titles:(NSMutableArray *)titles configration:(TYZPageConfigration *)config delegate:(id<TYZPageScrollMenuViewDelegate>)delegate currentIndex:(NSInteger)currentIndex
{
    frame.size.height = config.menuHeight;
    frame.size.width = config.menuWidth;
    
    TYZPageScrollMenuView *menuView = [[TYZPageScrollMenuView alloc] initWithFrame:frame];
    menuView.titles = titles;
    menuView.delegate = delegate;
    menuView.configration = config?:[TYZPageConfigration defaultConfigration];
    menuView.currentIndex = currentIndex;
    menuView.itemsArray = @[].mutableCopy;
    menuView.badgesArray = @[].mutableCopy;
    menuView.itemsWidthArray = @[].mutableCopy;
    [menuView setupSubViews];
    
    return menuView;
}


/**
 根据标题修下标修改标题
 
 @param title 标题
 @param index index
 */
- (void)updateTitle:(NSString *)title index:(NSInteger)index
{
    if (index < 0 || index > self.titles.count - 1)
    {
        return;
    }
    
    if (title.length == 0)
    {
        return;
    }
    
    [self reloadWithView];
}

/**
 根据标题数组刷新标题
 
 @param titles 标题数组
 */
- (void)updateTitles:(NSArray *)titles
{
    if (self.titles.count != self.titles.count)
    {
        return;
    }
    
    [self reloadWithView];
}

/**
 根据下标调整Item位置
 
 @param index 下标
 */
- (void)adjustItemPositionWithCurrentIndex:(NSInteger)index
{
    if (self.scrollView.contentSize.width != self.scrollView.width + 20)
    {
        UIButton *button = self.itemsArray[index];
        CGFloat offset = button.centerX - self.scrollView.width * 0.5;
        offset = offset > 0 ? offset : 0;
        
        CGFloat maxOffsetX = self.scrollView.contentSize.width - self.scrollView.width;
        maxOffsetX = maxOffsetX > 0 ?maxOffsetX:0;
        
        offset = offset > maxOffsetX?maxOffsetX:offset;
        
        [self.scrollView setContentOffset:CGPointMake(offset, 0) animated:YES];
    }
}

/**
 根据上个下标和当前点击的下标调整进度
 
 @param progress progress description
 @param lastIndex 上个下标index
 @param currentIndex 当前下标index
 */
- (void)adjustItemWithProgress:(CGFloat)progress lastIndex:(NSInteger)lastIndex currentIndex:(NSInteger)currentIndex
{
    self.lastIndex = lastIndex;
    self.currentIndex = currentIndex;
    
    if (lastIndex == currentIndex) return;
    UIButton *lastButton = self.itemsArray[self.lastIndex];
    UIButton *currentButton = self.itemsArray[self.currentIndex];
    
    /// 缩放系数
    if (self.configration.itemMaxScale > 1)
    {
        CGFloat scaleB = self.configration.itemMaxScale - self.configration.deltaScale * progress;
        CGFloat scaleS = 1 + self.configration.deltaScale * progress;
        lastButton.transform = CGAffineTransformMakeScale(scaleB, scaleB);
        currentButton.transform = CGAffineTransformMakeScale(scaleS, scaleS);
    }
    
    if (self.configration.showGradientColor)
    {
        /// 颜色渐变
        [self.configration setRGBWithProgress:progress];
        UIColor *norColor = [UIColor colorWithRed:self.configration.deltaNorR green:self.configration.deltaNorG blue:self.configration.deltaNorB alpha:1];
        UIColor *selColor = [UIColor colorWithRed:self.configration.deltaSelR green:self.configration.deltaSelG blue:self.configration.deltaSelB alpha:1];
        [lastButton setTitleColor:norColor forState:UIControlStateNormal];
        [currentButton setTitleColor:selColor forState:UIControlStateNormal];
        
    }
    else
    {
        if (progress > 0.5)
        {
            lastButton.selected = NO;
            currentButton.selected = YES;
            [lastButton setTitleColor:self.configration.normalItemColor forState:UIControlStateNormal];
            [currentButton setTitleColor:self.configration.selectedItemColor forState:UIControlStateNormal];
            currentButton.titleLabel.font = self.configration.selectedItemFont;
        }
        else if (progress < 0.5 && progress > 0)
        {
            lastButton.selected = YES;
            [lastButton setTitleColor:self.configration.selectedItemColor forState:UIControlStateNormal];
            lastButton.titleLabel.font = self.configration.selectedItemFont;
            currentButton.selected = NO;
            [currentButton setTitleColor:self.configration.normalItemColor forState:UIControlStateNormal];
            currentButton.titleLabel.font = self.configration.itemFont;
        }
    }
    
    if (progress > 0.5)
    {
        lastButton.titleLabel.font = self.configration.itemFont;
        currentButton.titleLabel.font = self.configration.selectedItemFont;
    }
    else if (progress < 0.5 && progress > 0)
    {
        lastButton.titleLabel.font = self.configration.selectedItemFont;
        currentButton.titleLabel.font = self.configration.itemFont;
    }
    
    CGFloat xD = 0;
    CGFloat wD = 0;
    if (!self.configration.scrollMenu &&
        !self.configration.aligmentModeCenter &&
        self.configration.lineWidthEqualFontWidth)
    {
        xD = currentButton.titleLabel.left + currentButton.left -( lastButton.titleLabel.left + lastButton.left );
        wD = currentButton.titleLabel.width - lastButton.titleLabel.width;
    }
    else
    {
//        if (_configration.lineWidth != 0)
//        {
//            xD = currentButton.left + (currentButton.width - self.configration.lineWidth) * 0.5 - lastButton.left;
//            wD = currentButton.width - lastButton.width;
//        }
//        else
//        {
            xD = currentButton.left - lastButton.left;
            wD = currentButton.width - lastButton.width;
//        }
    }
    
    /// 线条
    if (self.configration.showScrollLine)
    {
        
        if (!self.configration.scrollMenu &&
            !self.configration.aligmentModeCenter &&
            self.configration.lineWidthEqualFontWidth)
        { /// 处理Line宽度等于字体宽度
//            debugLog(@"处理Line宽度等于字体宽度");
            if (_configration.lineWidth != 0)
            {
                self.lineView.left = lastButton.left + (lastButton.width - self.configration.lineWidth) * 0.5 + xD * progress;
                self.lineView.width = self.configration.lineWidth;
            }
            else
            {
                self.lineView.left = lastButton.left + ([lastButton width]  - ([self.itemsWidthArray[lastButton.tag] floatValue])) / 2 - self.configration.lineLeftAndRightAddWidth + xD *progress;
                
                self.lineView.width = [self.itemsWidthArray[lastButton.tag] floatValue] + self.configration.lineLeftAndRightAddWidth *2 + wD *progress;
            }
        }
        else
        {
//            debugLog(@"线条else");
//            debugLog(@"xD*progress=%.2f", xD * progress);
            if (_configration.lineWidth != 0)
            {
                self.lineView.left = lastButton.left + xD * progress + (lastButton.width - self.configration.lineWidth) * 0.5;
                self.lineView.width = self.configration.lineWidth;
            }
            else
            {
                self.lineView.left = lastButton.left + xD *progress - self.configration.lineLeftAndRightAddWidth + self.configration.lineLeftAndRightMargin;
                self.lineView.width = lastButton.width + wD *progress + self.configration.lineLeftAndRightAddWidth *2 - 2 * self.configration.lineLeftAndRightMargin;
            }
        }
    }
    /// 遮盖
    if (self.configration.showConver)
    {
        self.converView.left = lastButton.left + xD *progress - kTYZPageScrollMenuViewConverMarginX;
        self.converView.width = lastButton.width  + wD *progress + kTYZPageScrollMenuViewConverMarginW;
        
        if (!self.configration.scrollMenu &&
            !self.configration.aligmentModeCenter &&
            self.configration.lineWidthEqualFontWidth)
        { /// 处理cover宽度等于字体宽度
            self.converView.left = lastButton.left + ([lastButton width]  - ([self.itemsWidthArray[lastButton.tag] floatValue])) / 2 -  kTYZPageScrollMenuViewConverMarginX + xD *progress;
            self.converView.width = [self.itemsWidthArray[lastButton.tag] floatValue] + kTYZPageScrollMenuViewConverMarginW + wD *progress;
        }
    }
}

/**
 选中下标
 
 @param index 选中的index
 @param animated animated description
 */
- (void)selectedItemIndex:(NSInteger)index animated:(BOOL)animated
{
    self.currentIndex = index;
    [self adjustItemAnimate:animated];
}

/**
 调整item
 
 @param animated animated description
 */
- (void)adjustItemWithAnimated:(BOOL)animated
{
    if (self.lastIndex == self.currentIndex)
        return;
    [self adjustItemAnimate:animated];
}

/**
 调整item
 
 @param animate animate description
 */
- (void)adjustItemAnimate:(BOOL)animate
{
    UIButton *lastButton = self.itemsArray[self.lastIndex];
    UIButton *currentButton = self.itemsArray[self.currentIndex];
    [UIView animateWithDuration:animate ? 0.3 : 0 animations:^{
        /// 缩放
        if (self.configration.itemMaxScale > 1)
        {
            lastButton.transform = CGAffineTransformMakeScale(1, 1);
            currentButton.transform = CGAffineTransformMakeScale(self.configration.itemMaxScale, self.configration.itemMaxScale);
        }
        /// 颜色
        [self.itemsArray enumerateObjectsUsingBlock:^(UIButton  * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.selected = NO;
            [obj setTitleColor:self.configration.normalItemColor forState:UIControlStateNormal];
            obj.titleLabel.font = self.configration.itemFont;
            if (idx == self.itemsArray.count - 1)
            {
                currentButton.selected = YES;
                [currentButton setTitleColor:self.configration.selectedItemColor forState:UIControlStateNormal];
                currentButton.titleLabel.font = self.configration.selectedItemFont;
            }
        }];
        
        /// 线条
        if (self.configration.showScrollLine)
        {
            if (self.configration.lineWidth != 0)
            {
                self.lineView.left = currentButton.left + (currentButton.width - self.configration.lineWidth) * 0.5;
                self.lineView.width = self.configration.lineWidth;
            }
            else
            {
                self.lineView.left = currentButton.left - self.configration.lineLeftAndRightAddWidth + self.configration.lineLeftAndRightMargin;
                self.lineView.width = currentButton.width + self.configration.lineLeftAndRightAddWidth * 2 - 2 * self.configration.lineLeftAndRightMargin;
            }
            
            if (!self.configration.scrollMenu &&
                !self.configration.aligmentModeCenter &&
                self.configration.lineWidthEqualFontWidth)
            {//处理Line宽度等于字体宽度
                if (self.configration.lineWidth != 0)
                {
                    self.lineView.left = currentButton.left + (currentButton.width - self.configration.lineWidth) * 0.5;
                    self.lineView.width = self.configration.lineWidth;
                }
                else
                {
                    self.lineView.left = currentButton.left + ([currentButton width]  - ([self.itemsWidthArray[currentButton.tag] floatValue])) / 2 - self.configration.lineLeftAndRightAddWidth;
                    self.lineView.width = [self.itemsWidthArray[currentButton.tag] floatValue] + self.configration.lineLeftAndRightAddWidth *2;
                }
            }
        }
        /// 遮盖
        if (self.configration.showConver)
        {
            self.converView.left = currentButton.left - kTYZPageScrollMenuViewConverMarginX;
            self.converView.width = currentButton.width +kTYZPageScrollMenuViewConverMarginW;
            /// 处理conver宽度等于字体宽度
            if (!self.configration.scrollMenu&&!self.configration.aligmentModeCenter&&self.configration.lineWidthEqualFontWidth)
            {
                self.converView.left = currentButton.left + ([currentButton width]  - ([self.itemsWidthArray[currentButton.tag] floatValue])) / 2  - kTYZPageScrollMenuViewConverMarginX;
                self.converView.width = [self.itemsWidthArray[currentButton.tag] floatValue] +kTYZPageScrollMenuViewConverMarginW;
            }
        }
        self.lastIndex = self.currentIndex;
    } completion:^(BOOL finished) {
        [self adjustItemPositionWithCurrentIndex:self.currentIndex];
    }];
}

/**
 刷新视图
 */
- (void)reloadWithView
{
    [self removeAllSubviews];
    
    [self.scrollView removeAllSubviews];
    
    [self.itemsArray removeAllObjects];
    [self.itemsWidthArray removeAllObjects];
    
    [self setupSubViews];
}

/// 更新badge
/// @param badgeValue badgeValue description
/// @param index index description
- (void)updateBadge:(NSInteger)badgeValue index:(NSInteger)index
{
    if (!self.configration.isBadge)
    {
        return;
    }
    if (index >= self.badgesArray.count)
    {
        return;
    }
    UILabel *badgeLabel = self.badgesArray[index];
    if (badgeValue > 0)
    {
        UIButton *btn = self.itemsArray[index];
//        UILabel *badgeLabel = self.badgesArray[idx];
        badgeLabel.centerX = btn.right;
        badgeLabel.centerY = btn.top + 10;
        
        badgeLabel.hidden = NO;
        if (badgeValue <= 99)
        {
            badgeLabel.text = [NSString stringWithFormat:@"%zd", badgeValue];
        }
        else
        {
            badgeLabel.text = @"99+";
        }
    }
    else
    {
        badgeLabel.text = @"0";
        badgeLabel.hidden = YES;
    }
}

@end





























